<?php
$PARAM_hote = 'localhost';
$PARAM_utilisateur = 'root';
$PARAM_mot_passe = '';
$PARAM_nom_bd = 'serveurlicence';
$connexion = new PDO ( 'mysql:host=' . $PARAM_hote . ';dbname=' . $PARAM_nom_bd, $PARAM_utilisateur, $PARAM_mot_passe );
$soap = new SoapClient ( "https://www.ovh.com/soapi/soapi-re-1.63.wsdl" );
// login
$session = $soap->login ( "df44485-ovh", "53aCt5Se", "fr", false );
$vretour = array();
$value="";
$requete = 'SELECT id, nom, prenom FROM praticiens WHERE id>:id';
/* renvoie du résultat de la requête */
$result = $connexion->prepare( $requete );
$result->execute(array(':id'=>$_POST['id']));
$nbSmsPratMoisPrec =0;
$nbSmsPratMoisActu =0;
if (! $result) {
	$vretour .= '<tr>Aucune licence trouvée</tr>';
} else {
	while ( $data = $result->fetch ( PDO::FETCH_OBJ )) {
		$nbSmsPratMoisPrec = nbSmsPrat ( $data->id, "dernier", $soap, $session );
		$nbSmsPratMoisActu = nbSmsPrat ( $data->id, "courant", $soap, $session );
		if ($nbSmsPratMoisPrec != 0 || $nbSmsPratMoisActu != 0) {
			// on affiche seulement les praticiens ayant utilisé des SMS ce mois-ci ou le mois précédant
			$value .= "<form method='post' action='editClients.php'>
						<tr>
	        			<td>" . $data->id . "</td>
	        			<td>" . $data->nom . "</td>
	        			<td>" . $data->prenom . "</td>
	        			<td>" . $nbSmsPratMoisPrec ."</td>
	        			<td>" . $nbSmsPratMoisActu . "</td>
						<input type='hidden' name='idPrat' value='' . $data->id . ''/>
	        			<td><button type='submit' class='btn btn-primary'>Editer compte client</button></td>
						</tr>
						</form>";
			$vretour["value"]=utf8_encode_array($value);
			$vretour["id"]=utf8_encode_array($data->id);
			echo json_encode($vretour);
			$result->closeCursor ();
			return;
		}
	}
}


$soap->logout ( $session );
function nbSmsPrat($id, $type, $soap, $session) {
	$nbsms = 0;
	if ($type == "dernier") {
		$moisdernier = date ( 'Ym', mktime ( 0, 0, 0, date ( 'm' ) - 1, date ( 'd' ), date ( 'Y' ) ) );
	} else {
		if ($type == "courant") {
			$moisdernier = date ( 'Ym' );
		}
	}
	try {
		
		// echo "login successfull\n";
		
		// telephonySmsHistory
		$result = $soap->telephonySmsHistory ( $session, "sms-df44485-1", "", "$id", "", "", "", "", "", "" );
		// echo "telephonySmsHistory successfull\n";
		$size=count ( $result );
		for($i = 0; $i < $size; $i ++) {
			if (substr ( $result [$i]->date, 0, 6 ) == $moisdernier) {
				$nbsms ++;
			}
		}

		return $nbsms;
		
		// return ($i); // your code here ...
		
		// logout
		
		// echo "logout successfull\n";
	} catch ( SoapFault $fault ) {
		// echo $fault;
		return 'Erreur';
	}
}
function utf8_encode_array($array) {
	return utf8_encode($array);
}
?>